package logic

import (
	"context"
	"fmt"
	"user_srv/internal/svc"
	"user_srv/model/mysql"
	"user_srv/user"

	"github.com/zeromicro/go-zero/core/logx"
)

type RealnameLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewRealnameLogic(ctx context.Context, svcCtx *svc.ServiceContext) *RealnameLogic {
	return &RealnameLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *RealnameLogic) Realname(in *user.RealnameRequest) (*user.RealnameResponse, error) {
	// todo: add your logic here and delete this line

	ur := mysql.UserRealname{}

	err := ur.CheckoutRealname(in.IdCard, in.UserId)
	if err != nil {
		return nil, fmt.Errorf("实名失败")
	}
	if ur.ID > 0 {
		return nil, fmt.Errorf("实名失败")
	}
	/*if !pkg.RealName(in.Realname, in.IdCard) {
		return nil, fmt.Errorf("实名失败")
	}*/
	ur = mysql.UserRealname{
		UserId:   in.UserId,
		Realname: in.Realname,
		IdCard:   in.IdCard,
	}
	err = ur.Create()
	if err != nil {
		return nil, fmt.Errorf("实名失败")
	}

	return &user.RealnameResponse{RealnameId: int64(ur.ID)}, nil
}
